package com.zhejiangzhengyuan.municipal_common.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhejiangzhengyuan.municipal_common.entity.BridgeFileMapping;
import com.zhejiangzhengyuan.municipal_common.entity.BridgeGis;
import com.zhejiangzhengyuan.municipal_common.entity.File;
import com.zhejiangzhengyuan.municipal_common.entity.dto.FileDto;
import com.zhejiangzhengyuan.municipal_common.entity.vo.BridgeVo;
import com.zhejiangzhengyuan.municipal_common.mapper.BridgeFileMappingMapper;
import com.zhejiangzhengyuan.municipal_common.mapper.BridgeGisMapper;
import com.zhejiangzhengyuan.municipal_common.mapper.FileMapper;
import com.zhejiangzhengyuan.municipal_common.service.BridgeGisService;
import com.zhejiangzhengyuan.municipal_common.utils.FileDuplicateCode;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;
import java.util.Objects;

import static com.zhejiangzhengyuan.municipal_common.utils.DateFormatUtil.timeFormat;
import static com.zhejiangzhengyuan.municipal_common.utils.StaticConstants.WAY_MODULE;

/**
 * GIS数据库中桥梁表（一路一档建设） 服务实现类
 *
 * @author linju
 * @since 2019-11-22
 */
@Slf4j
@Transactional
@Service
public class BridgeGisServiceImpl extends ServiceImpl<BridgeGisMapper, BridgeGis>
    implements BridgeGisService {

  @Autowired private BridgeGisMapper bridgeGisMapper;
  @Autowired private FileMapper fileMapper;
  @Autowired private FileDuplicateCode fileDuplicateCode;
  @Autowired private BridgeFileMappingMapper bridgeFileMappingMapper;

  public IPage<BridgeGis> page(Long pageNo, Long pageSize) {
    IPage<BridgeGis> page = new Page<>(pageNo, pageSize);
    QueryWrapper<BridgeGis> wrapper = new QueryWrapper<>();
    QueryWrapper<BridgeGis> id = wrapper.orderByAsc("id");
    IPage<BridgeGis> bridgeGisIPage = bridgeGisMapper.selectPage(page, id);
    List<BridgeGis> records = bridgeGisIPage.getRecords();
    for (BridgeGis record : records) {
      Map<String, Double> byIdPoint = bridgeGisMapper.findByIdPoint(record.getId());
      record.setStartPoint(byIdPoint.get("start_x") + "," + byIdPoint.get("start_y"));
      record.setEndPoint(byIdPoint.get("end_x") + "," + byIdPoint.get("end_y"));
    }
    return bridgeGisIPage;
  }

  // 修改方法
  @Transactional(rollbackFor = Exception.class)
  @Override
  public int updateBridge(BridgeGis bridgeGis) {
    if (bridgeGisMapper.selectById(bridgeGis.getId()) == null) {
      return 0;
    }
    // 设置的更新时间
    String endPoint = bridgeGis.getEndPoint();
    String startPoint = bridgeGis.getStartPoint();
    bridgeGis.setEndPoint(null);
    bridgeGis.setStartPoint(null);
    bridgeGis.setUpdateTime(timeFormat(System.currentTimeMillis()));
    if (bridgeGisMapper.updateById(bridgeGis) > 0) {
      if (Strings.isBlank(startPoint)) {
        String[] split = startPoint.split(",");
        bridgeGisMapper.updateStartPoint(bridgeGis.getId(), split[0], split[1]);
      }

      if (Strings.isBlank(endPoint)) {
        String[] split = endPoint.split(",");
        bridgeGisMapper.updateEndPoint(bridgeGis.getId(), split[0], split[1]);
      }
      return 1;
    }
    return 0;
  }

  // 添加方法
  @Transactional(rollbackFor = Exception.class)
  public int saveBridge(BridgeGis bridge) {
    // longitude,latitude
    String[] split = bridge.getStartPoint().split(",");
    String[] split1 = bridge.getEndPoint().split(",");
    // 桥梁的坐标 后续修改
    bridge.setStartPoint(null);
    bridge.setEndPoint(null);
    bridge.setCreateTime(timeFormat(System.currentTimeMillis()));
    // 桥梁的更新时间
    bridge.setUpdateTime(timeFormat(System.currentTimeMillis()));
    if (bridgeGisMapper.insert(bridge) > 0) {
      // 插入成功后修改geometry
      return bridgeGisMapper.updatePoint(bridge.getId(), split[0], split[1], split1[0], split1[1]);
    } else {
      return 0;
    }
  }

  @Override
  public BridgeGis findPointByBridge(String startPoint, String endPoint) {
    if (Objects.isNull(startPoint)) {
      return null;
    }
    if (Objects.isNull(endPoint)) {
      return null;
    }

    String[] startSplit = startPoint.split(",");
    String[] endSplit = endPoint.split(",");
    return bridgeGisMapper.findPointByBridge(
        startSplit[0], startSplit[1], endSplit[0], endSplit[1]);
  }

  // 桥梁文件更新
  @Override
  public int updateBridgeFile(FileDto dto) {
    Long id = dto.getId();
    MultipartFile multipartFile = dto.getFile();
    if (multipartFile == null || id == null) {
      return 0;
    }
    if (Objects.isNull(bridgeGisMapper.selectById(id))) {
      return 0;
    }
    File file = new File();
    if (fileDuplicateCode.insertFileDuplicateCode(file, multipartFile, WAY_MODULE) > 0) {
      BridgeFileMapping bridgeFileMapping = new BridgeFileMapping();
      bridgeFileMapping.setCreateTime(timeFormat(System.currentTimeMillis()));
      bridgeFileMapping.setUpdateTime(timeFormat(System.currentTimeMillis()));
      bridgeFileMapping.setBridgeId(id);
      bridgeFileMapping.setFileId(file.getId());
      bridgeFileMappingMapper.insert(bridgeFileMapping);
    }
    return 1;
  }

  @Override
  public IPage<BridgeVo> selectAllBridge(Page<BridgeVo> page) {
    Page<BridgeVo> voPage = bridgeGisMapper.getVoPage(page);
    List<BridgeVo> records = voPage.getRecords();
    for (BridgeVo bridgeVo : records) {
      List<String> strings = fileMapper.selectByBid(bridgeVo.getId());
      if (strings != null && strings.size() != 0) {
        bridgeVo.setLink(strings);
      }
    }
    return voPage;
  }
}
